package com.banciyuan.bcywebview.biz.main.newdiscover.tuijian;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.biz.detail.goods.GoodsDetailActivity;
import com.banciyuan.bcywebview.biz.groupdetail.NewTeamDetailActivity;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.DiscoverChoiceItem;

/**
 * Created by squall on 2017/3/22.
 */

public class TuijianAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<DiscoverChoiceItem> dlist = new ArrayList<>();
    private Context mContext;

    public TuijianAdapter(List<DiscoverChoiceItem> dlist, Context mContext) {
        this.dlist = dlist;
        this.mContext = mContext;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? 0 : 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new EmptyHolder(View.inflate(mContext, R.layout.smooth_blank, null));
        } else {
            return new ItemHolder(View.inflate(mContext, R.layout.tuijian_layout_item, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == 1) {
            ItemHolder itemHolder = (ItemHolder) holder;
            final DiscoverChoiceItem discoverItem = dlist.get(position - 1);
            ImageLoader.getInstance().displayImage(discoverItem.getMulti(), itemHolder.img, ImageOptions.options);
            if (!TextUtils.isEmpty(discoverItem.getContent())) {
                itemHolder.plain.setText(Html.fromHtml(discoverItem.getContent()));
            }
            itemHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String sub = discoverItem.getType();
                    if ("coser".equals(sub)) {
                        DetailDataHelper.gotoCosDetail(mContext, discoverItem.getRp_id(), discoverItem.getCp_id(), false);
                    } else if ("illust".equals(sub)) {
                        DetailDataHelper.gotoDrawerDetail(mContext, discoverItem.getRp_id(), discoverItem.getDp_id(), false);
                    } else if ("group".equals(sub)) {
                        gotoUtil.gotoAct(mContext, NewTeamDetailActivity.class, discoverItem.getGid());
                    } else if ("group_post".equals(sub)) {
                        DetailDataHelper.gotoGroupDetail(mContext, discoverItem.getGid(), discoverItem.getPost_id(), false);
                    } else if ("daily".equals(sub)) {
                        DetailDataHelper.gotoDailyDetail(mContext, discoverItem.getUd_id(), false);
                    } else if ("novel".equals(sub)) {
                        DetailDataHelper.gotoWriterDetail(mContext, discoverItem.getRp_id(), discoverItem.getWp_id(), false);
                    } else if ("zhipin_goods".equals(sub)) {
                        gotoUtil.gotoAct(mContext, GoodsDetailActivity.class, discoverItem.getZg_id());
                    }
                }
            });
        } else {
            EmptyHolder emptyHolder = (EmptyHolder) holder;
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
            emptyHolder.blank.setLayoutParams(layoutParams);
        }
    }

    @Override
    public int getItemCount() {
        return dlist.size() + 1;
    }

    private class ItemHolder extends RecyclerView.ViewHolder {

        private ImageView img;
        private TextView plain;

        public ItemHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.tuijian_img);
            plain = (TextView) itemView.findViewById(R.id.tuijian_plain);
        }
    }

    private class EmptyHolder extends RecyclerView.ViewHolder {

        private View blank;

        public EmptyHolder(View itemView) {
            super(itemView);
            blank = itemView.findViewById(R.id.blank);
        }
    }
}
